package com.yanqu.road.server.manager.activity.xingyou.task;

import com.yanqu.road.entity.activity.commonteam.data.CommonTeamUserData;
import com.yanqu.road.entity.activity.youxing.CrossXingYouShanShuiUserData;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.activity.XingYouShanShuiProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.xingyou.CrossXingYouActivity;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.executor.GeneralTask;
import com.yanqu.road.utils.property.Property;

import java.util.Arrays;
import java.util.List;

public class CrossXingYouSendCaptorRewardTask extends GeneralTask {

    private long playerId;

    private int teamId;

    private List<CommonTeamUserData> memberList;

    private Property reward;

    private CrossXingYouActivity activity;

    public CrossXingYouSendCaptorRewardTask(long playerId, int teamId, List<CommonTeamUserData> memberList, Property reward, CrossXingYouActivity activity) {
        this.playerId = playerId;
        this.teamId = teamId;
        this.memberList = memberList;
        this.reward = reward;
        this.activity = activity;
    }

    @Override
    public void doRun() {

        synchronized (activity.getTeamLocker(teamId)) {
            for (CommonTeamUserData userData : memberList) {
                CrossXingYouShanShuiUserData shanShuiUserData = activity.getUserData(userData.getUserId());
                activity.addReward(shanShuiUserData, reward, false, 2);

                //展示用
                if(userData.getUserId() != playerId) {
                    if(shanShuiUserData.getCaptorReward() == null){
                        shanShuiUserData.setCaptorReward(new Property());
                    }
                    shanShuiUserData.getCaptorReward().addProperty(activity.getConfig().getSHANSHUI_SILVER_COIN_ID(), reward.getCountByGoodsId(activity.getConfig().getSHANSHUI_SILVER_COIN_ID()));
                    shanShuiUserData.getCaptorReward().addProperty(activity.getConfig().getSHANSHUI_GOLD_COIN_ID(), reward.getCountByGoodsId(activity.getConfig().getSHANSHUI_GOLD_COIN_ID()));
                }
                //下发榜单变更
                //同步下银票(临时热更处理)
                XingYouShanShuiProto.CrossShanShuiEnterMapRsp.Builder crossRsp = XingYouShanShuiProto.CrossShanShuiEnterMapRsp.newBuilder();
                XingYouShanShuiProto.ShanShuiEnterMapRsp.Builder respMsg = XingYouShanShuiProto.ShanShuiEnterMapRsp.newBuilder();
                respMsg.setRet(-10);
                crossRsp.setRsp(respMsg);
                crossRsp.setSilverCoinTotal(shanShuiUserData.getSilverCoinTotal());
                crossRsp.setRound(shanShuiUserData.getRound());
                crossRsp.setUseEnergyTotal(shanShuiUserData.getUseEnergy());
                MessageHelper.sendPacket(userData.getServerId(), userData.getUserId(), YanQuMessageUtils.buildMessage(Protocol.S_XING_YOU_SHAN_SHUI_ENTER_MAP_FROM_CROSS, crossRsp));
            }
        }
        int goldCoin = reward.getCountByGoodsId(activity.getConfig().getSHANSHUI_GOLD_COIN_ID()).intValue();
        if(goldCoin > 0) {
            //下发金页子奖励
            for (CommonTeamUserData userData : memberList) {
                XingYouShanShuiProto.CrossShanShuiAddGoldCoinReq.Builder sync = XingYouShanShuiProto.CrossShanShuiAddGoldCoinReq.newBuilder();
                sync.setNum(goldCoin);
                sync.setWay(eLogMoneyType.XingYouShanShuiCatchReward.getValue());
                MessageHelper.sendPacket(userData.getServerId(), userData.getUserId(), YanQuMessageUtils.buildMessage(Protocol.S_XING_YOU_SHAN_SHUI_ADD_GOLD_COIN_FROM_CROSS, sync));
            }
        }

        activity.syncShanShuiPropNotify(teamId, Arrays.asList(XingYouShanShuiProto.PROP.TEAM_SILVER_VALUE, XingYouShanShuiProto.PROP.TEAM_CUL_SILVER_VALUE, XingYouShanShuiProto.PROP.YA_MEN_SILVER_VALUE));

        activity.getNeedSyncRedDotTeamMap().add(teamId);
    }
}
